﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microhard.WeixinMP.Interfaces;
using Microhard.WeixinMP.DataAccess;
using Microhard.WeixinMP.DTO;
using Microhard.Core.DBUtility;
using Microhard.Core;
using Microhard.WeixinMP.Msg;

namespace Microhard.WeixinMP.Impls
{
    public class ArticleLinkImpl : IArticleShareLink
    {
        public Core.DBUtility.ExecutedResult Add(Models.ShareLinkModel model)
        {
            ExecutedResult rtn = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            var op = model.ToEntity();

            try
            {
                db.ShareLinks.AddObject(op);
                db.SaveChanges();
            }
            catch (System.Exception ex)
            {
                rtn.ErrorMsg.Add(DBOperationFailedStatus.AddFailed, ShareLinkMsg.AddFailed);
                Log.Error("IArticleShareLink.Add", ex);
            }
            return rtn;
        }

        public Core.DBUtility.ExecutedResult Update(Models.ShareLinkModel model)
        {
            ExecutedResult result = new ExecutedResult();
            MicrohardWXEntities db = new MicrohardWXEntities();
            var entity = db.ShareLinks.SingleOrDefault(l => l.ShareLinkID == model.ShareLinkID);
            if (entity != null)
            {
                entity.UpdateTime = DateTime.Now;
                entity.ClickCount = model.ClickCount;
            }
            try
            {
                db.SaveChanges();
            }
            catch (Exception ex)
            {
                result.ErrorMsg.Add(DBOperationFailedStatus.UpdateFailed, ShareLinkMsg.UpdateFailed);
                Log.Error("IArticleShareLink.Update", ex);
            }
            return result;
        }

        public Core.DBUtility.ExecutedResult Delete(List<Guid> ids)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ExecutedResult Delete(Models.ShareLinkModel model)
        {
            throw new NotImplementedException();
        }

        public Models.ShareLinkModel GetModelByID(Guid id)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            var fb = db.ShareLinks.SingleOrDefault(i => i.ShareLinkID == id);
            if (fb != null)
            {
                return fb.ToModel();
            }
            return null;
        }

        public Models.ShareLinkModel GetModelByModel(Models.ShareLinkModel model)
        {
            throw new NotImplementedException();
        }

        public List<Models.ShareLinkModel> Query(Models.ShareLinkModel Model, int pageIndex, int pageSize, out int totalCount)
        {
            throw new NotImplementedException();
        }

        public List<Models.ShareLinkModel> Query(Models.ShareLinkModel Model)
        {
            throw new NotImplementedException();
        }

        public Core.DBUtility.ScopeResult scope
        {
            set;
            get;
        }

        public bool IsPublisherClick(Models.ShareLinkModel model, out Guid shareLinkID)
        {
            shareLinkID = Guid.Empty;
            MicrohardWXEntities db = new MicrohardWXEntities();
            var cnt = db.ShareLinks.SingleOrDefault(s => s.ShareUserOpenID == model.WeixinName
                && s.ArticleID == model.ArticleID);
            if (cnt == null)
            {
                return false;
            }
            shareLinkID = cnt.ShareLinkID;
            return true;
        }


        public Models.ShareLinkModel GetModeByOAID(string openID, Guid articleID)
        {
            MicrohardWXEntities db = new MicrohardWXEntities();
            var entity = db.ShareLinks.SingleOrDefault(w => w.ShareUserOpenID == openID && w.ArticleID == articleID);
            if (entity != null)
                return entity.ToModel();
            return null;
        }
    }
}
